<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cate;
use Auth;

class CateController extends Controller
{
    protected $model_cate;

    protected $page;
    public function __construct(Cate $cate)
    {
        $this->model_cate = $cate;
        $this->page = 15;
    }

    public function index()
    {
        $list = $this->model_cate->getList($this->page);
        return view('admin.cate.cate_list', ['list' => $list]);
    }

    public function add()
    {
        return view('admin.cate.cate_add');
    }

    public function addSave(Request $request)
    {
        $messages = [
            'required' => ':attribute 必须填写。',
        ];
        $this->validate($request, [
            'name' => 'required',
            'description' => 'required',
        ], $messages);
        $request->user_name = Auth::user()->name;
        $request->user_id = Auth::id();
        $result = $this->model_cate->create($request);
        if ($result) {
            return redirect()->route('admin.cate.list');
        } else {
            // TODO
            dd('添加失败');
        }
    }

    public function del($id)
    {
        $result = $this->model_cate->del($id);
        if ($result) {
            return redirect()->route('admin.cate.list');
        } else {
            // TODO
            dd('删除失败');
        }
    }
}
